<template>
  <div>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-input placeholder="请输入内容"  v-model="search" style="width: 200px;">
          </el-input>
          
          <el-button type="primary" @click="showCateDialog()" style="  margin-left: 5rem;"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        class="treetable"
        :data="catelist.filter(
          (data) =>
            !search ||
            data.cat_name.toLowerCase().includes(search.toLowerCase())
        )"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        index-text="#"
        border
      >
      
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === true"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <!--  eslint-disable-next-line vue/no-unused-vars -->
        <template slot="opt" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="editCateDialog(scope.row.cat_id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            @click="deleteCateById(scope.row.cat_id)"
            >删除</el-button
          >
          <!-- <pre>{{scope.row}}</pre> -->
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <!-- 添加分类的表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            expand-trigger="hover"
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChanged"
            clearable="true"
            change-on-select="true"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类对话框 -->
    <el-dialog
      title="编辑分类"
      :visible.sync="editCateDialogVisible"
      width="50%"
    >
      <el-form
        ref="editCateFormRef"
        :model="editCateForm"
        :rules="editCateFormRules"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCateSuccess()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      search:'',
      // 分页查询条件
      querInfo: {
        type: 3,
        // 当前页码值默认为1
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 2,
      },
      catelist: [
        {
          id: 1,
          cat_name: "手机",
          cat_deleted: true,
          cat_level: 0,
          children: [
            {
              cat_level: 1,
              cat_deleted: true,
              cat_name: "vivo",
            },
            {
              cat_level: 1,
              cat_deleted: true,
              cat_name: "oppo",
            },
            {
              cat_level: 1,
              cat_deleted: true,
              cat_name: "华为",
              children: [
                {
                  cat_level: 2,
                  cat_deleted: true,
                  cat_name: "p系列",
                },
                {
                  cat_level: 2,
                  cat_deleted: true,
                  cat_name: "Mate系列",
                },
                {
                  cat_level: 2,
                  cat_deleted: true,
                  cat_name: "nova系列",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          cat_name: "服装",
          cat_deleted: true,
          cat_level: 0,
            children: [
                {
                  cat_level: 1,
                  cat_deleted: true,
                  cat_name: "男装",
                },
                {
                  cat_level: 1,
                  cat_deleted: true,
                  cat_name: "女装",
                },
               
              ],
        },
        {
          id: 3,
          cat_name: "电脑",
          cat_deleted: true,
          cat_level: 0,
        },
      ],
      total: 0,
      // 添加分类对话框的显示与隐藏
      addCateDialogVisible: false,
      // 添加分类表单数据对象
      addCateForm: {
        // 将要添加的分类名称
        cat_name: "",
        // 父级分类的id
        cat_pid: 0,
        // 分类的等级，默认要添加的是1一级分类
        cat_level: 0,
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //   为table指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          //   将当前列自定义为模板列
          type: "template",
          //   当前这一列使用的模板名称
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
        },
      ],
      // 父级分类列表
      parentCateList: [
        {
          cat_pid: "1",
          cat_name: "vivo",
          children: [
            {
              cat_pid: "1",
              cat_name: "vivo男",
              children: [
                {
                  cat_pid: "1",
                  cat_name: "vivo男",
                },
                {
                  cat_pid: "2",
                  cat_name: "vivo女",
                },
              ],
            },
            {
              cat_pid: "2",
              cat_name: "vivo女",
            },
          ],
        },
        {
          cat_pid: "2",
          cat_name: "oppo",
          children: [
            {
              cat_pid: "1",
              cat_name: "oppo男",
            },
            {
              cat_pid: "2",
              cat_name: "oppo女",
            },
          ],
        },
        {
          cat_pid: "3",
          cat_name: "apple",
          children: [
            {
              cat_pid: "1",
              cat_name: "apple男",
            },
            {
              cat_pid: "2",
              cat_name: "apple女",
            },
          ],
        },
        {
          cat_pid: "4",
          cat_name: "华为",
          children: [
            {
              cat_pid: "1",
              cat_name: "华为男",
            },
            {
              cat_pid: "2",
              cat_name: "华为女",
            },
          ],
        },
        {
          cat_pid: "5",
          cat_name: "小米",
          children: [
            {
              cat_pid: "1",
              cat_name: "小米男",
            },
            {
              cat_pid: "2",
              cat_name: "小米女",
            },
          ],
        },
      ],
      // 指定级联选择器的配置对象
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      // 选中的父级分类的ID数组
      selectedKeys: [],
      editCateDialogVisible: false,
      // 编辑分类信息
      editCateForm: {},
      editCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
    };
  },
  // created () {
  //   this.getCateList()
  // },
  methods: {
    //   获取商品分类数据
    // async getCateList () {
    //   // 查询参数params
    //   const { data: res } = await this.$http.get('categories', {
    //     params: this.querInfo
    //   })
    //   if (res.meta.status !== 200) {
    //     return this.$message.error('获取分类列表失败')
    //   }
    //   // console.log(res.data)
    //   //   把数据列表赋值给分类列表
    //   this.catelist = res.data.result
    //   //   为总数据条数赋值
    //   this.total = res.data.total
    // },
    // 监听pagesize改变
    handleSizeChange() {
      //   this.querInfo.pagesize = newSize
      //   this.getCateList()
    },
    // // 监听pagenum页码值页面
    handleCurrentChange() {
      //   this.querInfo.pagenum = newPage
      //   this.getCateList()
    },
    // 添加分类对话框
    showCateDialog() {
      this.getParentCateList();
      this.addCateDialogVisible = true;
    },
    getParentCateList() {
      //   const { data: res } = await this.$http.get('categories', {
      //     params: { type: 2 }
      //   })
      //   if (res.meta.status !== 200) {
      //     return this.$message.error('获取数据失败')
      //   }
      //   // console.log(res.data)
      //   this.parentCateList = res.data
    },
    // 选择项发生变化触发函数
    parentCateChanged() {
      // console.log(this.selectedKeys)
      // 如果selectedKeys数组中的length大于0，证明选中的父级分类
      if (this.selectedKeys.length > 0) {
        // 父级分类的ID
        this.addCateForm.cat_pid =
          this.selectedKeys[this.selectedKeys.length - 1];
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length;
        // eslint-disable-next-line no-useless-return
        return;
      } else {
        // 父级分类的id
        this.addCateForm.cat_pid = 0;
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = 0;
      }
    },
    addCate() {
      // console.log(this.addCateForm)
      // this.$refs.addCateFormRef.validate(async valid => {
      //   if (!valid) return
      //   const { data: res } = await this.$http.post(
      //     'categories',
      //     this.addCateForm
      //   )
      //   if (res.meta.status !== 201) {
      //     return this.$message.error('提交信息失败')
      //   }
      //   this.$message.success('提交信息成功')
      //   this.getCateList()
      this.addCateDialogVisible = false;
      // })
    },
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields();
      this.selectedKeys = [];
      this.selectedKeys.cat_pid = 0;
      this.selectedKeys.cat_level = 0;
    },
    // 编辑分类
    editCateDialog() {
      // console.log(id)
      // const { data: res } = await this.$http.get("categories/" + id);
      // // console.log(res)
      // if (res.meta.status !== 200) {
      //   return this.$message.error("获取分类信息失败");
      // }
      // this.editCateForm = res.data;
      this.editCateDialogVisible = true;
    },
    // 编辑分类信息成功提交
    editCateSuccess() {
      // this.$refs.editCateFormRef.validate(async valid => {
      //   if (!valid) return
      //   const { data: res } = await this.$http.put(
      //     'categories/' + this.editCateForm.cat_id,
      //     { cat_name: this.editCateForm.cat_name }
      //   )
      //   // console.log(res)
      //   if (res.meta.status !== 200) {
      //     return this.$message.error('修改分类信息失败')
      //   }
      //   this.$message.success('修改分类信息成功')
      //   this.getCateList()
        this.editCateDialogVisible = false
      // })
    },
    // 删除分类信息
    // async deleteCateById (id) {
    //     const confirmResult = await this.$confirm(
    //       '此操作将永久删除该分类, 是否继续?',
    //       '删除分类信息提示',
    //       {
    //         confirmButtonText: '确定',
    //         cancelButtonText: '取消',
    //         type: 'warning'
    //       }
    //     ).catch(err => err)
    //     // console.log(confirmResult)
    //     if (confirmResult !== 'confirm') {
    //       return this.$message.info('已取消删除')
    //     }
    //     const { data: res } = await this.$http.delete('categories/' + id)
    //     // console.log(res)
    //     if (res.meta.status !== 200) {
    //       return this.$message.error('删除分类信息失败')
    //     }
    //     this.$message.success('删除分类信息成功')
    //     this.getCateList()
    //   }
  },
};
</script>
<style scoped>
.treetable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
